package com.hc.dfs.handle;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.hc.dfs.activity.DocumentDetailsActivity;
import com.hc.dfs.activity.R;
import com.hc.dfs.activity.ManageActivity;
import com.hc.dfs.activity.SigningOperationActivity;
import com.hc.dfs.dailog.DelayDialog;
import com.hc.dfs.service.MainService;
import com.hc.dfs.task.Task;

/**
 * @author 王洪胜 E-mail:
 * @version 创建时间：2013-2-25 上午8:31:31
 * 
 *          类说明：公文签批列表中的条目具体操作处理类，包括点击、长按。
 * 
 */
public class ManageTabItem implements OnItemClickListener,
		OnItemLongClickListener, OnCreateContextMenuListener {

	// 主activity
	private ManageActivity mainActivity;
	// pageId
	private int pageId;
	// 传递长按的条目位置
	private static int p;

	public ManageTabItem(Context context, int pageId) {
		mainActivity = (ManageActivity) context;
		this.pageId = pageId;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		switch (pageId) {
		case 0:
			menu.add(0, 0, 0, "签批公文");
			menu.add(0, 1, 1, "申请延时");
			menu.add(0, 2, 2, "查看路径");
			break;
		case 1:
			menu.add(0, 0, 0, "跟踪进度");
			menu.add(0, 1, 1, "查看路径");
			break;
		case 2:
			menu.add(0, 0, 0, "删除公文");
			break;
		default:
			break;
		}

	}

	// 处理条目选择事件
	public void onContextItemSelected(MenuItem item) {
		switch (pageId) {
		case 0:
			if (item.getItemId() == 0) {
				System.out.println(p + "签批公文");
				mainActivity.startActivity(new Intent(mainActivity,
						SigningOperationActivity.class));
			}
			if (item.getItemId() == 1) {
				System.out.println(p + "申请延时");
				new DelayDialog(R.layout.delay_dialog, mainActivity);
			}
			if (item.getItemId() == 2) {
				System.out.println(p + "查看路径");
			}
			break;
		case 1:
			if (item.getItemId() == 0) {
				System.out.println(p + "跟踪进度");
			}
			if (item.getItemId() == 1) {
				System.out.println(p + "查看路径");
			}
			break;
		case 2:
			if (item.getItemId() == 0) {
				System.out.println(p + "删除公文");
			}
			break;
		default:
			break;
		}

	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		// TODO Auto-generated method stub
		System.out.println("长按啦长按啦");
		p = position;
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		System.out.println("输出位置" + position);
		ListView listView = (ListView) parent;
		HashMap<String, Object> map = (HashMap<String, Object>) listView.getItemAtPosition(position);
		// 获取公文id
		int docId = (Integer) map.get("docId");
		// 添加不同类型公文根据ID查询公文详情任务
		MainService.newTask(new Task(Task.GET_DOCUMENT_DETAIL, docId));
		//通过intent传递点击的条目所在頁碼及公文类型
		int docType = (Integer) map.get("docType");
		 String doctype = docType + "";
		 String page = pageId + "";
		 Intent intent = new Intent();
		// intent.putExtra("docId", docid);
		 intent.putExtra("docType", doctype);
		 intent.putExtra("page", page);
//		switch (docType) {
//		case 4:
//			MainService.newTask(new Task(Task.GET_CHENGPIDAN_DETAIL, docId));
//			break;
//		case 5:
//			MainService.newTask(new Task(Task.GET_FAWENGAO_DETAIL, docId));
//			break;
//		case 6:
//			MainService.newTask(new Task(Task.GET_HETONG_DETAIL, docId));
//			break;
//
//		default:
//			break;
//		}
		// 通过intent切换至公文详情activity
//		switch (pageId) {
//		case 0:
//			intent.setClass(mainActivity, DetailsUnsignedActivity.class);
//			break;
//		case 1:
//			intent.setClass(mainActivity, DetailsFollowActivity.class);
//			break;
//		case 2:
//			intent.setClass(mainActivity, DetailsToDoActivity.class);
//			break;
//		default:
//			break;
//		}
		intent.setClass(mainActivity, DocumentDetailsActivity.class);
		mainActivity.startActivity(intent);
		// 在多个activity组成的activityGroup中，通过父activity切换动画
		// 第一个参数为启动时动画效果，第二个参数为退出时动画效果
		mainActivity.getParent().overridePendingTransition(
				R.anim.translate100_0_300, R.anim.translate0_0);
	}

}
